//
//  PackageDetailViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/26.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "PackageDetailViewController.h"
#import "ProductTableViewCell.h"
#import "ProductInfo.h"
#import "Context.h"
#import "PurchaseDetailViewController.h"

@interface PackageDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *priceLabel;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIButton *selectButton;
@property (strong, nonatomic) NSMutableArray *itemArray;

@end

@implementation PackageDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    [self setBackNavgationItem];
    self.navigationItem.title = @"套餐详情";
    
    [self createUI];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark- UIButton Action
- (void)selectButtonAction {
    PurchaseDetailViewController *purchaseDetailViewController = [[PurchaseDetailViewController alloc] init];
    if(self.packageInfo != nil) {
        [Context sharedInstance].selectPackageId = self.packageInfo.packageId;
        purchaseDetailViewController.packageInfo = self.packageInfo;
        purchaseDetailViewController.commodityInfo = self.commodityInfo;
    }
    [self.navigationController pushViewController:purchaseDetailViewController animated:YES];
}

#pragma mark - UITableViewDelegate && UITableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = self.itemArray != nil ? self.itemArray.count : 0;
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = TRANS_VALUE(80.0f);
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat height = 0.0f;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    view.backgroundColor = RGBCOLOR(247, 247, 247);
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"ProductTableViewCell";
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell) {
        cell = [[ProductTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    if(indexPath.row < [self.itemArray count]) {
        //TOOD -- 发现列表
        ProductInfo *prodcutInfo = (ProductInfo *)[self.itemArray objectAtIndex:indexPath.row];
        if(prodcutInfo.imageUrl != nil) {
            cell.imageUrl = prodcutInfo.imageUrl;
        }
        NSString *productName = prodcutInfo.productName != nil ? prodcutInfo.productName : @"";
        cell.productName = productName;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //TODO -- 跳转到产品详情页面
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row < self.itemArray.count) {
        if([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        if([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    } else {
        if([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        if([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }
}

#pragma mark - Private Method
- (void)createUI {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TRANS_VALUE(32.0f))];
    headerView.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:headerView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.0f), TRANS_VALUE(0.0f), TRANS_VALUE(100.0f), TRANS_VALUE(32.0f))];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    self.titleLabel.textColor = I_COLOR_BLACK;
    self.titleLabel.text = @"优惠套餐1";
    [headerView addSubview:self.titleLabel];
    
    self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(100.0f), TRANS_VALUE(0.0f), TRANS_VALUE(100.0f), TRANS_VALUE(32.0f))];
    self.priceLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(13.0f)];
    self.priceLabel.textColor = I_COLOR_RED;
    self.priceLabel.text = @"￥20000";
    [headerView addSubview:self.priceLabel];
    
    UIView *dividerView = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(32.0f) - 0.5f, SCREEN_WIDTH, 0.5f)];
    dividerView.backgroundColor = I_DIVIDER_COLOR;
    [headerView addSubview:dividerView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64.0f - TRANS_VALUE(44.0f)) style:UITableViewStylePlain];
    self.tableView.backgroundColor = I_COLOR_WHITE;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[ProductTableViewCell class] forCellReuseIdentifier:@"ProductTableViewCell"];
    self.tableView.showsVerticalScrollIndicator = NO;
    //设置tableview分割线
    if([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    self.selectButton = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 64.0f - TRANS_VALUE(44.0f), SCREEN_WIDTH, TRANS_VALUE(44.0f))];
    self.selectButton.backgroundColor = I_COLOR_RED;
    self.selectButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(15.0f)];
    [self.selectButton setTitleColor:I_COLOR_WHITE forState:UIControlStateNormal];
    [self.selectButton setTitleColor:I_COLOR_WHITE forState:UIControlStateSelected];
    [self.selectButton setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.selectButton setTitle:@"立即购买" forState:UIControlStateSelected];
    [self.view addSubview:self.selectButton];
    [self.selectButton addTarget:self action:@selector(selectButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)loadData {
    if(!self.itemArray) {
        self.itemArray = [[NSMutableArray alloc] init];
    }
    if(self.packageInfo != nil) {
        self.titleLabel.text = self.packageInfo.packageDesc;
        NSString *priceStr = self.packageInfo.price != nil ? self.packageInfo.price : @"";
        self.priceLabel.text = [NSString stringWithFormat:@"￥%@", priceStr];
         if(self.packageInfo.productArray != nil)
        [self.itemArray addObjectsFromArray:self.packageInfo.productArray];
    }
    [self.tableView reloadData];
}

@end
